package com.insurance.product.service.impl;

import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.product.enums.ProductStateEnum;
import com.insurance.product.service.ProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;

import com.insurance.product.dao.ProductDao;
import com.insurance.product.entity.ProductEntity;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
        );

        return new PageUtils(page);
    }

    public QueryWrapper<ProductEntity> getQueryWrapper(){
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<ProductEntity>();
        queryWrapper.eq("delete_flag", DeleteFlagEnum.UN_DELETE.getCode());
        queryWrapper.eq("state", ProductStateEnum.UP.getCode());
        return queryWrapper;
    }

    public QueryWrapper<ProductEntity> getQueryWrapper(ProductEntity productEntity){
        QueryWrapper<ProductEntity> queryWrapper = getQueryWrapper();
        if(!StringUtils.isBlank(productEntity.getProductCode())){
            queryWrapper.eq("product_code", productEntity.getProductCode());
        }
        return queryWrapper;
    }

    /**
     * 通过产品编码查询产品
     *
     * @param productCode 产品编码
     * @return
     */
    @Override
    public ProductEntity getProductByCode(String productCode){
        if(StringUtils.isBlank(productCode)){
            return null;
        }
        ProductEntity productEntity = new ProductEntity();
        productEntity.setProductCode(productCode);
        QueryWrapper<ProductEntity> queryWrapper = getQueryWrapper(productEntity);
        return this.getOne(queryWrapper);
    }

}